#!/usr/bin/env perl
# License: GNU GPL 2
# Author: Muhammad Ichsan <ichsan@gmail.com>

$db = "test";
$db_user = "root";
$db_pwd = "rootPwd";

sub prepare_tables {
  local($lang, $translator, $copyright, $sqlfile);
  $lang = shift;
  $translator = shift;
  $copyright = shift;
  $sqlfile = shift;

  $sql = "
SET NAMES UTF8;

DROP TABLE IF EXISTS quran_chapters_$lang;
CREATE TABLE quran_chapters_$lang (
  number int(11) NOT NULL default '0',
  title varchar(100) NOT NULL,
  meaning varchar(100),
  PRIMARY KEY (number)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS quran_verses_$lang;
CREATE TABLE quran_verses_$lang (
  id int(11) NOT NULL auto_increment,
  chapter_number int(11) NOT NULL default '0',
  number int(3) NOT NULL default '0',
  content text NOT NULL,
  PRIMARY KEY (id)
) ENGINE=MyISAM COMMENT='by:$translator, c:$copyright' DEFAULT CHARSET=utf8;
";

  open(SQL_FILE, ">>$sqlfile");
  print SQL_FILE $sql;
  close(SQL_FILE);

  return 1;
}

sub import_tables {
  local($lang, $file);
  $lang = shift;
  $file = shift;

  system("mysql -u $db_user -p$db_pwd $db < $file > /tmp/silent");

  return 1
}

sub export_tables {
  my $lang = shift;

  system("mysqldump --skip-add-locks -u $db_user -p$db_pwd $db quran_chapters_$lang quran_verses_$lang|bzip2 > quran-$lang.mysql.bz2");

  return 1;
}
